/*     */ package com.zimbra.qa.unittest.prov.ldap;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException.LdapSizeLimitExceededException;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.GenericLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.ZAttributes;
/*     */ import com.zimbra.cs.ldap.ZLdapContext;
/*     */ import com.zimbra.cs.ldap.ZLdapFilter;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory;
/*     */ import com.zimbra.cs.ldap.ZLdapFilterFactory.FilterId;
/*     */ import com.zimbra.cs.ldap.ZMutableEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchControls;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEntry;
/*     */ import com.zimbra.cs.ldap.ZSearchResultEnumeration;
/*     */ import com.zimbra.cs.ldap.ZSearchScope;
/*     */ import java.io.PrintStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class LdapExample
/*     */ {
/*     */   private LdapServerConfig.GenericLdapConfig getLdapConfig()
/*     */   {
/*  38 */     String ldapUrl = "ldapi:///";
/*  39 */     boolean startTLSEnabled = false;
/*  40 */     String bindDN = "cn=config";
/*  41 */     String bindPassword = LC.ldap_root_password.value();
/*     */     
/*  43 */     return new LdapServerConfig.GenericLdapConfig(ldapUrl, startTLSEnabled, bindDN, bindPassword);
/*     */   }
/*     */   
/*     */   public void getAttributes() throws Exception
/*     */   {
/*  48 */     LdapServerConfig.GenericLdapConfig ldapConfig = getLdapConfig();
/*     */     
/*  50 */     ZLdapContext zlc = null;
/*     */     try {
/*  52 */       zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  57 */       ZAttributes attrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", new String[] { "zimbraId", "cn", "description" });
/*  58 */       String zimbraId = attrs.getAttrString("zimbraId");
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  63 */       allAttrs = zlc.getAttributes("cn=default,cn=cos,cn=zimbra", null);
/*     */     }
/*     */     finally {
/*     */       ZAttributes allAttrs;
/*  67 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   public void search() throws Exception {
/*  72 */     String base = "cn=servers,cn=zimbra";
/*  73 */     String filter = "(objectClass=zimbraServer)";
/*  74 */     String[] returnAttrs = { "objectClass", "cn" };
/*     */     
/*  76 */     ZLdapFilter zFilter = ZLdapFilterFactory.getInstance().fromFilterString(ZLdapFilterFactory.FilterId.ZMCONFIGD, filter);
/*     */     
/*  78 */     ZSearchControls searchControls = ZSearchControls.createSearchControls(ZSearchScope.SEARCH_SCOPE_SUBTREE, 0, returnAttrs);
/*     */     
/*     */ 
/*     */ 
/*  82 */     LdapServerConfig.GenericLdapConfig ldapConfig = getLdapConfig();
/*     */     
/*  84 */     ZLdapContext zlc = null;
/*     */     try {
/*  86 */       zlc = LdapClient.getContext(ldapConfig, LdapUsage.SEARCH);
/*     */       
/*  88 */       ZSearchResultEnumeration ne = zlc.searchDir(base, zFilter, searchControls);
/*  89 */       while (ne.hasMore()) {
/*  90 */         ZSearchResultEntry entry = ne.next();
/*     */         
/*  92 */         String dn = entry.getDN();
/*     */         
/*  94 */         ZAttributes attrs = entry.getAttributes();
/*  95 */         String cn = attrs.getAttrString("cn");
/*  96 */         String[] objectClasses = attrs.getMultiAttrString("objectClass");
/*     */         
/*  98 */         System.out.println("dn = " + dn);
/*  99 */         System.out.println("cn = " + cn);
/* 100 */         for (String objectClass : objectClasses) {
/* 101 */           System.out.println("objetClass = " + objectClass);
/*     */         }
/*     */       }
/* 104 */       ne.close();
/*     */     }
/*     */     catch (LdapException.LdapSizeLimitExceededException e) {
/* 107 */       e.printStackTrace();
/* 108 */       throw e;
/*     */     }
/*     */     finally {
/* 111 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   public void createEntry() throws Exception
/*     */   {
/* 117 */     String dn = "uid=user1,ou=people,dc=test,dc=com";
/*     */     
/* 119 */     LdapServerConfig.GenericLdapConfig ldapConfig = getLdapConfig();
/*     */     
/* 121 */     ZLdapContext zlc = null;
/*     */     try {
/* 123 */       zlc = LdapClient.getContext(ldapConfig, LdapUsage.ADD);
/*     */       
/* 125 */       ZMutableEntry entry = LdapClient.createMutableEntry();
/* 126 */       entry.setDN(dn);
/*     */       
/* 128 */       entry.addAttr("objectClass", "inetOrgPerson");
/* 129 */       entry.addAttr("objectClass", "zimbraAccount");
/* 130 */       entry.addAttr("objectClass", "amavisAccount");
/*     */       
/* 132 */       entry.setAttr("uid", "user1");
/* 133 */       entry.setAttr("cn", "user1");
/* 134 */       entry.setAttr("sn", "lastname");
/* 135 */       entry.setAttr("zimbraAccountStatus", "active");
/* 136 */       entry.setAttr("zimbraId", "ba6198a3-bb49-4425-94b0-d4e9354e87b5");
/* 137 */       entry.addAttr("mail", "user1@trest.com");
/* 138 */       entry.addAttr("mail", "user-one@test.com");
/*     */       
/* 140 */       zlc.createEntry(entry);
/*     */     }
/*     */     finally {
/* 143 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   public void deleteEntry() throws Exception {
/* 148 */     String dn = "uid=user1,ou=people,dc=test,dc=com";
/*     */     
/* 150 */     LdapServerConfig.GenericLdapConfig ldapConfig = getLdapConfig();
/*     */     
/* 152 */     ZLdapContext zlc = null;
/*     */     try {
/* 154 */       zlc = LdapClient.getContext(ldapConfig, LdapUsage.DELETE);
/* 155 */       zlc.deleteEntry(dn);
/*     */     }
/*     */     finally {
/* 158 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   public void modifyEntry() throws Exception {
/* 163 */     String dn = "cn=config,cn=zimbra";
/* 164 */     LdapServerConfig.GenericLdapConfig ldapConfig = getLdapConfig();
/*     */     
/* 166 */     ZLdapContext zlc = null;
/*     */     try {
/* 168 */       zlc = LdapClient.getContext(ldapConfig, LdapUsage.MOD);
/*     */       
/* 170 */       ZMutableEntry entry = LdapClient.createMutableEntry();
/* 171 */       entry.setAttr("description", "so this gets modified");
/* 172 */       zlc.replaceAttributes(dn, entry.getAttributes());
/*     */     }
/*     */     finally {
/* 175 */       LdapClient.closeContext(zlc);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 181 */     LdapClient.initialize();
/*     */     
/* 183 */     LdapExample test = new LdapExample();
/*     */     
/* 185 */     test.getAttributes();
/* 186 */     test.search();
/* 187 */     test.createEntry();
/* 188 */     test.deleteEntry();
/* 189 */     test.modifyEntry();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/prov/ldap/LdapExample.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */